rm(list=ls())

#Ns=c(30,50,100,200,300,400,500,600,800,1000)
#Ks=c(1,2,3,4,5,6,7,8,9,10)

# these Ns and K's are used in SM's fig2.
Ns=c(100,200,300,400,500)
Ks=c(1,2,3,5,8,10) # corresponding to k=2,3,4,6,9,11 in SM (they count T)


datsim=read.table("NK.out")

dim(datsim)

Dat={}

for(n in 1:length(Ns)){
  for(k in 1:length(Ks)){
    dat=datsim[datsim[,1]==Ns[n]&datsim[,2]==Ks[k],]
    Dat=rbind(Dat,apply(dat,2,mean))
  }
}

dim(Dat)

N=Dat[,1]
K=Dat[,2]
nearest=Dat[,4]  # mean(dist from cf to nearest match);

nearestk<-matrix(0,length(Ns), length(Ks))
for(k in 1:length(Ks)){
nearestk[,k]=nearest[K==Ks[k]]
}

pdf(file="fig1.pdf")

op <- par(mfrow = c(1, 1), cex=1.2)

matplot(Ns,nearestk,type="l",xlab="Number of Data Points",ylab="Average Distance to the Nearest Match")
text(130,0.14,expression(k==2),cex=.8)
text(130,.4,expression(k==3),cex=.8)
text(130,.7,expression(k==4),cex=.8)
text(130,1.3,expression(k==6),cex=.8)
text(130,2.1,expression(k==9),cex=.8) 
text(130,2.55,expression(k==11),cex=.8)

par(op)

dev.off()
q()
